package com.foo.cms.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: LZS
 * Date: 11-7-6
 * Time: 下午1:47
 */
public abstract class BaseService {
    private static final Logger logger = LoggerFactory.getLogger(BaseService.class);

    @Resource
    protected NamedParameterJdbcTemplate jdbcTemplate;

    protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    protected static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");


    protected String getType(Object o) {
        String value = String.valueOf(o);
        return "A".equalsIgnoreCase(value) ? "文章模型" :
                "V".equalsIgnoreCase(value) ? "视频模型" :
                        "P".equalsIgnoreCase(value) ? "图片模型" :
                                "I".equalsIgnoreCase(value) ? "图集模型" :
                                        "S".equalsIgnoreCase(value) ? "分类模型" : "未知模型";
    }

    protected String getPartName(Object partId) {
        String partName = "";
        if (partId != null) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("partId", partId);
            Map<String, Object> resultMap = jdbcTemplate.queryForMap("select vc2partname from dt_part where numpartid = :partId", params);
            if (resultMap != null && !resultMap.isEmpty()) {
                partName = (String) resultMap.get("vc2partname");
            }
        }
        return partName;
    }

    protected String getStatus(Object o) {
        String value = String.valueOf(o);
        return "1".equals(value) ? "待审核" : "2".equals(value) ? "审核不通过" : "3".equals(value) ? "审核通过" : "4".equals(value) ? "已发布" : "未知";
    }

    protected boolean isBlank(String value) {
        return value == null || "".equals(value.trim());
    }

    public boolean validateFileType(MultipartFile file) {
        if (file != null && file.getSize() > 0) {
            List<String> allowType = Arrays.asList("image/jpg", "image/jpeg", "image/png", "image/gif");
            return allowType.contains(file.getContentType().toLowerCase());
        }
        return false;
    }
}
